﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using HalconDotNet;
using MvCamCtrl.NET;
using Platform.DAL;
using VisionDemo.Application.CameraModels.Models;

namespace VisionDemo.Application.CameraModels.ViewModels
{
    public class CameraViewModel : ObservableRecipient
    {
        private HikHelper hikHelper = new HikHelper();
        private string _CameraName;

        public string CameraName
        {
            get { return _CameraName; }
            set { SetProperty(ref _CameraName, value); }
        }
        public ObservableCollection<CameraRunModel> CameraRunList { get; set; } =
            new ObservableCollection<CameraRunModel>();
        public RelayCommand SearchCameraCommand { get; set; }
        public RelayCommand OpenCameraCommand { get; set; }
        public RelayCommand CloseCameraCommand { get; set; }
        public RelayCommand ContinuesModeCommand { get; set; }
        public RelayCommand TriggerModeCommand { get; set; }
        public RelayCommand StartGrabCommand { get; set; }
        public RelayCommand StopGrabCommand { get; set; }
        public RelayCommand TriggerExecCommand { get; set; }
        public RelayCommand<bool> SoftTriggerCommand { get; set; }
        private HObject _DisplayImage;

        public HObject DisplayImage
        {
            get { return _DisplayImage; }
            set { SetProperty(ref _DisplayImage, value); }
        }
        

        /// <summary>
        /// 显示区域
        /// </summary>
        private HObject _DisplayIcon;
        public HObject DisplayIcon
        {
            get => _DisplayIcon;
            set
            {
                _DisplayIcon = value;
                OnPropertyChanged();
            }
        }

        /// <summary>
        /// 显示文字
        /// </summary>
        private string _DisplayText;
        public string DisplayText
        {
            get => _DisplayText;
            set
            {
                _DisplayText = value;
                OnPropertyChanged();
            }
        }
        public ObservableCollection<string> CameraList { get; set; } =
            new ObservableCollection<string>();

        public CameraViewModel()
        {
            
            hikHelper.ImageEvent += HikHelper_ImageEvent;

            SearchCameraCommand = new RelayCommand(() =>
            {
                hikHelper.InitialCameraList();
                if (hikHelper.DeviceList.Count > 0)
                {
                    foreach (var camera in hikHelper.DeviceList)
                    {
                        CameraList.Add(camera);
                    }
                    CameraName = CameraList[0];
                }
            });
            OpenCameraCommand = new RelayCommand(() =>
            {
                if (
                    CameraName == null
                    || hikHelper.DeviceList.Count < 0
                    || !hikHelper.DeviceList.Contains(CameraName)
                )
                {
                    AddLog(1, "请先选择要打开的相机");
                    return;
                }

                //加载相机列表
                hikHelper.LoadCameraList(new List<string>() { CameraName });

                //打开相机
                var result = hikHelper.OpenCamera();

                if (result[0].IsSuccess)
                {
                    AddLog(0, result[0].Message);

                    //this.tb_ExposureTime.Value = Convert.ToInt32(hikHelper.ExposureTime[0]);
                    //this.txt_ExposureTime.Text = hikHelper.ExposureTime[0].ToString("f1");

                    //this.tb_Gain.Value = Convert.ToInt32(hikHelper.Gain[0]);
                    //this.txt_Gain.Text = hikHelper.Gain[0].ToString("f1");

                    //this.tb_FrameRate.Value = Convert.ToInt32(hikHelper.FrameRate[0]);
                    //this.txt_FrameRate.Text = hikHelper.FrameRate[0].ToString("f1");
                }
                else
                {
                    AddLog(1, result[0].Message);
                }
            });
            CloseCameraCommand = new RelayCommand(() =>
            {
                hikHelper.StopGrab();
                //this.btn_StartGrab.Enabled = true;
                //this.btn_StopGrab.Enabled = false;

                hikHelper.CloseCamera();
                AddLog(0, "相机关闭成功");
            });
            ContinuesModeCommand = new RelayCommand(() =>
            {
                if (hikHelper.SetTriggerMode(MyCamera.MV_CAM_TRIGGER_MODE.MV_TRIGGER_MODE_OFF) == 0)
                {
                    //this.chk_SoftTrigger.Enabled = false;
                    //this.btn_TriggerExec.Enabled = false;
                    AddLog(0, "设置连续模式成功");
                }
            });
            TriggerModeCommand = new RelayCommand(() =>
            {
                if (hikHelper.SetTriggerMode(MyCamera.MV_CAM_TRIGGER_MODE.MV_TRIGGER_MODE_ON) == 0)
                {
                    AddLog(0, "设置触发模式成功");
                }
            });

            StartGrabCommand = new RelayCommand(() =>
            {
                hikHelper.StartGrab();

                AddLog(0, "开始采集");
            });
            StopGrabCommand = new RelayCommand(() => {

                hikHelper.StopGrab();

                AddLog(0, "停止采集");
            });
            TriggerExecCommand = new RelayCommand(() => 
            {
                hikHelper.TriggerExec();
            });
            SoftTriggerCommand = new RelayCommand<bool>((bool obj) => {
                if (obj)
                {
                    hikHelper.SetTriggerSource(MyCamera.MV_CAM_TRIGGER_SOURCE.MV_TRIGGER_SOURCE_SOFTWARE);
                }
                else
                {
                    hikHelper.SetTriggerSource(MyCamera.MV_CAM_TRIGGER_SOURCE.MV_TRIGGER_SOURCE_LINE0);
                }

            });
        }

        private void HikHelper_ImageEvent(
            MvCamCtrl.NET.MyCamera.MV_DISPLAY_FRAME_INFO stDisplayInfo
        )
        {
            var hObject=new  HObject();
            HOperatorSet.GenImage1Extern(
                out hObject,
                "byte",
                stDisplayInfo.nWidth,
                stDisplayInfo.nHeight,
                stDisplayInfo.pData,
                IntPtr.Zero
            );
            DisplayImage = hObject;
        }

        private void AddLog(int v1, string v2)
        {
            switch (v1)
            {
                case 0:
                    CameraRunList.Add(
                        new CameraRunModel()
                        {
                            Color = "Green",
                            Message = DateTime.Now.ToString() + $":    {v2}",
                        }
                    );
                    break;
                case 1:
                    CameraRunList.Add(
                        new CameraRunModel()
                        {
                            Color = "Red",
                            Message = DateTime.Now.ToString() + $":    {v2}",
                        }
                    );
                    break;
                default:
                    break;
            }
        }
    }
}
